/**
Copyright [2009] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package cruiseprojectlist.service;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import cruiseprojectlist.domain.ProjectsStorageBucket;

public class ProjectListService {

	public char[] getProjectListAsString() {
		Set<String> projects = getProjectList();
		String result = "";
		for (String project : projects) {
			result += project + "<br>";
		}
		return result.toCharArray();
	}

	public Set<String> getProjectList() {
		Set<String> result = new TreeSet<String>();

		DocumentBuilder docBuilder = docBuilderLoad();

		Document doc = parseFile(docBuilder);

		if (null != doc)
			getResult(result, doc);

		return result;
	}

	private void getResult(Set<String> result, Document doc) {
		NodeList listOfprojects = doc.getElementsByTagName("project");
		// int totalProjects = listOfprojects.getLength();
		// System.out.println("Total no of projects : " + totalProjects);

		for (int s = 0; s < listOfprojects.getLength(); s++) {
			Node firstProjectNode = listOfprojects.item(s);
			NamedNodeMap nnm = firstProjectNode.getAttributes();
			// System.out.println(nnm.getNamedItem("name").getNodeValue());
			String projectName = nnm.getNamedItem("name").getNodeValue();
			result.add(projectName);
		}
	}

	private Document parseFile(DocumentBuilder docBuilder) {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService
				.getProjectsStorageBucket();
		Document doc = null;
		if (null != projectStorageBucket.getCruiseConfigPath()) {
			if (new File(projectStorageBucket.getCruiseConfigPath()).exists()) {
				try {
					doc = docBuilder.parse(new File(projectStorageBucket
							.getCruiseConfigPath()));
				} catch (SAXException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return doc;
	}

	private DocumentBuilder docBuilderLoad() {
		DocumentBuilder docBuilder = null;
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return docBuilder;
	}

}
